package com.mofofa.maozy.components.dialog

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.core.widget.NestedScrollView
import androidx.viewpager.widget.PagerAdapter
import com.mofofa.maozy.R
import com.mofofa.maozy.extend.recordMaozyClickMaozyEvent


/** 结果页展示view pager 适配器 */
class PermissionsAdapter(context: Context, arrayList: List<PermissionItem>) :
    PagerAdapter() {

    private val mContext: Context
    private val dataList: List<PermissionItem>

    init {
        mContext = context
        dataList = arrayList
    }

    /**
     * 点击回调
     *
     * @return Int 位置
     * @return RevObjAiHomeDemo 值
     * @return Int 类型
     */
    private var mItemClickListener: (Int, PermissionItem, Int) -> Unit =
        { action, obj, position -> }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        //子View显示
        val item = dataList[position]
        var view: View =
            View.inflate(mContext, R.layout.view_maozy_permission_phone_state, null)
        var mPermissionActionDeny: TextView =
            view.findViewById(R.id.textPermissionPhoneStateActionDeny)
        var mPermissionActionAgree: TextView =
            view.findViewById(R.id.textPermissionPhoneStateActionAgree)
        var mPermissionScrollView: NestedScrollView =
            view.findViewById(R.id.scrollPermissionPhoneStateContent)
        var llPermissionAgreeContent: LinearLayout =
            view.findViewById(R.id.llPermissionPhoneStateAgreeContent)
        var llPermissionActionContent: LinearLayout =
            view.findViewById(R.id.llPermissionPhoneStateActionContent)

        if (item.type == PermissionItem.TYPE_SMS) {
            view = View.inflate(mContext, R.layout.view_maozy_permission_sms, null)
            mPermissionActionDeny = view.findViewById(R.id.textPermissionSmsActionDeny)
            mPermissionActionAgree = view.findViewById(R.id.textPermissionSmsActionAgree)
            mPermissionScrollView = view.findViewById(R.id.scrollPermissionSmsContent)
            llPermissionAgreeContent = view.findViewById(R.id.llPermissionSmsAgreeContent)
            llPermissionActionContent = view.findViewById(R.id.llPermissionSmsActionContent)

        }
        if (item.type == PermissionItem.TYPE_APP) {
            view = View.inflate(mContext, R.layout.view_maozy_permission_app, null)
            mPermissionActionDeny = view.findViewById(R.id.textPermissionAppActionDeny)
            mPermissionActionAgree = view.findViewById(R.id.textPermissionAppActionAgree)
            mPermissionScrollView = view.findViewById(R.id.scrollPermissionAppContent)
            llPermissionAgreeContent = view.findViewById(R.id.llPermissionAppAgreeContent)
            llPermissionActionContent = view.findViewById(R.id.llPermissionAppActionContent)
        }

        //判断是否已经授权
        if (item.result == PermissionItem.RESULT_AGREE) {
            llPermissionAgreeContent.visibility = View.VISIBLE
            llPermissionActionContent.visibility = View.GONE
        } else {
            llPermissionAgreeContent.visibility = View.GONE
            llPermissionActionContent.visibility = View.VISIBLE
        }

        println("holder------ type: ${item.type}  position: $position result: ${item.result}")

        //更新点击展示
        if (item.hadReadContent) {
            mPermissionActionAgree.setBackgroundResource(R.drawable.maozy_permission_agree)
        }

        //滑动到底监听
        mPermissionScrollView.setOnScrollChangeListener { v: NestedScrollView, _: Int, scrollY: Int, _: Int, _: Int ->
            try {
                val edg = v.getChildAt(0).measuredHeight - v.measuredHeight
                if (scrollY >= edg) {
                    item.hadReadContent = true
                    mPermissionActionAgree.setBackgroundResource(R.drawable.maozy_permission_agree)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                item.hadReadContent = true
                mPermissionActionAgree.setBackgroundResource(R.drawable.maozy_permission_agree)
            }
        }

        //点击事件
        mPermissionActionDeny.setOnClickListener {
            mItemClickListener.invoke(PermissionItem.RESULT_DENY, item, position)
            //打点
            mPermissionActionDeny.recordMaozyClickMaozyEvent()
        }
        mPermissionActionAgree.setOnClickListener {
            if (item.hadReadContent) {
                mItemClickListener.invoke(PermissionItem.RESULT_AGREE, item, position)
            } else {
                Toast.makeText(
                    mContext,
                    mContext.getString(R.string.maozy_scroll_to_bottom_tips),
                    Toast.LENGTH_SHORT
                ).show()
            }
            //打点
            mPermissionActionAgree.recordMaozyClickMaozyEvent()
        }


        //添加到父控件
        container.addView(view)
        return view
    }

    override fun getItemPosition(obj: Any): Int {
//        if (dataList.isEmpty()) {
//            return POSITION_NONE;
//        }
        return POSITION_NONE;
    }

    override fun getCount(): Int {
        return dataList.size
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view === `object`
    }

    override fun destroyItem(container: ViewGroup, position: Int, obj: Any) {
        //从viewpager中移除掉
        container.removeView(obj as View)
    }

    /**
     * 设置点击回调
     *
     * @param Int 位置
     * @param RevObjAiHomeDemo 值
     * @param Int 类型
     */
    fun setItemClickListener(listener: (Int, PermissionItem, Int) -> Unit) {
        mItemClickListener = listener
    }
}